#!/usr/bin/python

import os;
import sys;
from threading import Thread;
#from datetime import datetime;
import time;
import struct;
from socket import *;

class SocketConnector(Thread):
    host = "127.0.0.1";
    port = 1234;
    repeat = 1000;
    def __init__(self, host="", port=1234, repeat = 1000):
        self.host = host;
        self.port = port;
        self.repeat = repeat;
        Thread.__init__(self);
        pass;
    def test_ice(self):
        ice_sock = socket(AF_INET, SOCK_STREAM);
        ice_sock.setsockopt(SOL_SOCKET, SO_LINGER, struct.pack('ii', 1,0));
        ice_sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1);
        ice_sock.connect((self.host, self.port));
        ice_sock.send('hello,world');
        content = ice_sock.recv(1024);
        #print content + "\n";
        ice_sock.close();
        pass;

    def run(self):
        for i in range(0, self.repeat):
            try:
                self.test_ice();
            except :
                print 'failed to connect';

        #print self.host;
if __name__ == '__main__':
    print 'hello';
    q = [];
    begin_time = time.time();
    for i in range(0, 100):
        th = SocketConnector("127.0.0.1", 1234, 10000);
        q.append(th);
        th.start();
    for x in q:
        x.join();
    end_time = time.time();
    print end_time - begin_time;
    #print end_time.minute*60 + end_time.second - begin_time.minute*60 - begin_time.second; 
